package com.example.bbs.ui.activity;

import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.bbs.R;
import com.example.bbs.base.Data;
import com.example.bbs.bean.MediaData;
import com.example.bbs.bean.User;
import com.example.bbs.config.HttpConstant;
import com.example.bbs.ui.view.RichTextView;
import com.example.bbs.util.BaseUtil;
import com.example.bbs.util.HttpUtil;

import java.util.concurrent.CompletableFuture;

import okhttp3.FormBody;

public class MyOwnInfoActivity extends AppCompatActivity {

    private AlertDialog dlg;
    private TextView tv_user;
    private AlertDialog dlg_per;
    private TextView tv_per;
    private TextView tv_email;
    private ImageView iv_avatar;

    static final String LOG_TAG = PersonalInfo.class.getSimpleName();

    Button btn_edit;//修改资料

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ImageView my_btn_user;//姓名
        ImageView my_btn_per;//简介
        setContentView(R.layout.activity_my_own_info);
        iv_avatar = findViewById(R.id.my_iv_avatar);
        tv_user = findViewById(R.id.my_tv_user);
        tv_per = findViewById(R.id.my_tv_per);
        tv_email = findViewById(R.id.my_tv_email);
        my_btn_user = findViewById(R.id.my_btn_user);
        my_btn_per = findViewById(R.id.my_btn_per);
        btn_edit = findViewById(R.id.my_btn_edit);
        Listener listener = new Listener();
        my_btn_user.setOnClickListener(listener);
        my_btn_per.setOnClickListener(listener);
        btn_edit.setOnClickListener(listener);
    }

    class Listener implements View.OnClickListener {

        @SuppressLint("NonConstantResourceId")
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.my_btn_user:
                    View dlgview = LayoutInflater.from(MyOwnInfoActivity.this).inflate(R.layout.dialog_layout, null);
                    EditText etname = dlgview.findViewById(R.id.etname);
                    Button btnok = dlgview.findViewById(R.id.btnok);
                    btnok.setOnClickListener(view13 -> {
                        tv_user.setText(etname.getText().toString());
                        dlg.dismiss();
                    });
                    dlg = new AlertDialog.Builder(MyOwnInfoActivity.this)
                            .setView(dlgview)
                            .create();
                    dlg.show();
                    break;
                case R.id.my_btn_per:
                    View dlgview_per = LayoutInflater.from(MyOwnInfoActivity.this).inflate(R.layout.dialog_layout, null);
                    EditText etname_per = dlgview_per.findViewById(R.id.etname);
                    Button btnok_per = dlgview_per.findViewById(R.id.btnok);
                    btnok_per.setOnClickListener(view1 -> {
                        tv_per.setText(etname_per.getText().toString());
                        dlg_per.dismiss();
                    });
                    dlg_per = new AlertDialog.Builder(MyOwnInfoActivity.this)
                            .setView(dlgview_per)
                            .create();
                    dlg_per.show();
                    break;
                case R.id.my_btn_edit:
                    Data.myself.setName(tv_user.getText().toString());
                    Data.myself.setIntroduction(tv_per.getText().toString());
                    Log.i(LOG_TAG, JSON.toJSONString(Data.myself));
                    HttpUtil.postAsync(HttpConstant.REQUEST_UPDATE_USER, JSON.toJSONString(Data.myself), (response) -> {
                        if (response != null  && response.getBoolean(HttpConstant.FIELD_REPLY))
                            Log.i(LOG_TAG, "update success");
                        else
                            Log.w(LOG_TAG, "update failure");
                    });
                    break;
            }
        }
    }

    private void load() {
        if (TextUtils.isEmpty(String.valueOf(Data.myself.getId()))){
            BaseUtil.showToast("please login");
        }
        else {
            get_personal_info(Data.myself.getId());
        }
    }

    private void get_personal_info(int id){
        FormBody formBody = new FormBody.Builder()
                .add(HttpConstant.FIELD_USERID, "" + id)
                .build();
        JSONObject json = HttpUtil.post(HttpConstant.REQUEST_GET_USER, formBody);
        if (json == null || !json.getBoolean(HttpConstant.FIELD_REPLY))
            return;
        JSONObject result = json.getJSONObject(HttpConstant.FIELD_DATA);
        Log.d(LOG_TAG, "" + result);
        String name = result.getString("name");
        String introduction = result.getString("introduction");
        String email = result.getString("email");
        tv_user.setText(name);
        tv_per.setText(introduction);
        tv_email.setText(email);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(LOG_TAG, "onResume");

        try {
            CompletableFuture.runAsync(this::load).get(); // 初始化
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(LOG_TAG, "onPause");
    }
}